// pages/market/coupou/detail.js
import message from '../../../utils/message'
import api from '../../../utils/api'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        
        coupou_id:null,
        uid:null,
        has_load: false,
        coupou_detail:null

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {

        if(options.id){
            var uid = null;
            if(options.uid){
                uid = options.uid
            }
            this.setData({
                coupou_id: options.id,
                uid:uid
            })

            //this.getCoupouDetail();
        }else{
            message.show_error("优惠券ID不存在");
        }

    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

        //this.getCoupouDetail();

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

        this.getCoupouDetail();

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function ( res ) {

        if(res.from=='button'){
            console.log(res.target)
        }

        var share_info = this.data.coupou_detail.share_info;


        if(share_info){
            return share_info;
        }

    },
    getCoupouDetail:function () {
        var me = this;

        var coupou_id = this.data.coupou_id;

        if(coupou_id==null){
            message.show_error( "优惠券ID不存在" );
            return ;
        }
        
        var params = {}
        params.coupou_id = this.data.coupou_id;
        params.uid = this.data.uid
        message.show_loading();
        api.coupou.getCoupouDetail( params , function (response) {
            message.hide_loading();

            if(response.status==1){
                me.setData({
                    coupou_detail: response.data,
                    has_load:true
                })
            }else{

                message.show_error( response.message )

            }
        })
    },
    getCoupou:function () {
        //领取优惠券
        var me = this;
        var params = {}
        params.id = this.data.coupou_id;


        api.user.getCoupou( params ,function (response) {
            if(response.status==1){

                message.success(response.message , function (  ) {
                    me.getCoupouDetail();
                })
                
            }else{
                message.show_error(response.messagge)
            }
            
        })
    },
    goBack:function (  ) {
        wx.navigateBack({
          delta: 1,
        })
        
    }
})